package com.vnp.vchat.version2;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.turbomanage.httpclient.HttpResponse;
import com.vnp.vchat.ChatActivity;
import com.vnp.vchat.MainActivity;
import com.vnp.vchat.R;
import com.vnp.vchat.R.layout;
import com.vnp.vchat.Util.Common;
import com.vnp.vchat.Util.PreferenceHelper;
import com.vnp.vchat.Util.Utils;
import com.vnp.vchat.adapter.SearchAdapter;
import com.vnp.vchat.bean.ObjContact;
import com.vnp.vchat.bean.ObjKhachhang;
import com.vnp.vchat.httpclient.HttpBasicClientHelper;
import com.vnp.vchat.httpclient.HttpClientHelper;
import com.vnp.vchat.httpclient.MyJsonAsyncCallback;
import com.vnp.vchat.httpclient.MyJsonHttpResponseHandler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DanhbaActivity extends Activity {

	TextView title;
	ListView listContact;
	PreferenceHelper preferenceHelper;
	String hash;
	
	SearchAdapter adapter;
	
	ArrayList<ObjKhachhang> objKhachhangs = new ArrayList<ObjKhachhang>();
	
	int page = 0;
	
	RelativeLayout llProgressBar;
	TextView txtNoContact;
	ProgressBar progressBar;
	
	private int preLast;
	boolean isLoading = false;
	Handler mHandler = new Handler();
	
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			getDanhba(++page);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_danhba);
		
		preferenceHelper = new PreferenceHelper(this);
		
		hash = Utils.MD5(Common.KEY_HTTP+"|"+preferenceHelper.getUserId());
		
		title = (TextView)findViewById(R.id.title);
		listContact = (ListView) findViewById(R.id.list_contact);
		txtNoContact = (TextView) findViewById(R.id.txt_no_contact);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		
		llProgressBar = (RelativeLayout) findViewById(R.id.ll_progress_bar);
		
		adapter = new SearchAdapter(DanhbaActivity.this, objKhachhangs);
		listContact.setAdapter(adapter);
		
		init();
		setFont();
		
		getDanhba(page);
		
		listContact.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				int lastItem  = firstVisibleItem + visibleItemCount;
				if (lastItem == totalItemCount){
					if (preLast != lastItem) {
						int last_pos = listContact.getLastVisiblePosition();
						if (last_pos >= objKhachhangs.size()-1){
							llProgressBar.setVisibility(View.VISIBLE);
							mHandler.removeCallbacks(r);
							mHandler.postDelayed(r, 500);
						}
						
						preLast = lastItem;
					}
					
				}
			}
		});
		
		listContact.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				MainActivity.onGoto = true;
				Intent c = new Intent(DanhbaActivity.this,ChatNewActivity.class);
				objKhachhangs.get(position).setUrlDangxem("");
				c.putExtra("khachhang", objKhachhangs.get(position));
				startActivity(c);
			}
			
		});
	}
	
	
	void init(){
		title.setText("Danh bạ");
	}
	
	void setFont(){
		title.setTypeface(Utils.getTypefaceBold(DanhbaActivity.this));
	}
	
	void getDanhba(int page){
		
		HttpBasicClientHelper httpBasicClientHelper = new HttpBasicClientHelper(new MyJsonAsyncCallback(){

			@Override
			public void onSuccess(HttpResponse httpResponse, JSONObject re) {
				int statusCode = httpResponse.getStatus();
				if (statusCode == 200){
					try {
						int status = re.getInt("status");
						
						runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								progressBar.setVisibility(View.GONE);
								llProgressBar.setVisibility(View.GONE);
							}
						});
						
						
						if (status == 1){
							JSONArray ja = re.getJSONArray("data");
							
							Log.i("DATA", "DANH BA: "+ja.toString());
							
							if (ja.length() > 0){
								for (int i = 0; i < ja.length(); i++) {
									JSONObject jo = ja.getJSONObject(i);
									
									int id = jo.getInt("id");
									String name = jo.getString("name");
									String avatar = jo.getString("avatar");
									String contact = jo.getString("contact");
//									String from = jo.getString("from");
									
									ObjKhachhang objKhachhang = new ObjKhachhang();
									objKhachhang.setId(String.valueOf(id));
									objKhachhang.setName(name);
									objKhachhang.setUrlAvatar(avatar);
									objKhachhang.setLoginName(contact);
									objKhachhang.setUrlDangxem("");
									
									objKhachhangs.add(objKhachhang);
								}
								
								adapter.notifyDataSetChanged();
								
							} else {
								runOnUiThread(new Runnable() {
									
									@Override
									public void run() {
										txtNoContact.setVisibility(View.VISIBLE);
									}
								});
							}
							
							
						} else {
							runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									progressBar.setVisibility(View.GONE);
									llProgressBar.setVisibility(View.GONE);
									txtNoContact.setVisibility(View.VISIBLE);
								}
							});
						}
						
					} catch (JSONException e) {
						e.printStackTrace();
					}
				} else {
					runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							progressBar.setVisibility(View.GONE);
							llProgressBar.setVisibility(View.GONE);
							txtNoContact.setVisibility(View.VISIBLE);
						}
					});
				}
			}

			@Override
			public void onFailure(Exception e) {
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						progressBar.setVisibility(View.GONE);
						llProgressBar.setVisibility(View.GONE);
						txtNoContact.setVisibility(View.VISIBLE);
					}
				});
			}
			
		});
		
		httpBasicClientHelper.getContact(preferenceHelper.getAdminId(), preferenceHelper.getHashSetting(), page);
		
	}
	
	
}
